<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>上传</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<style>
			img {
				width: 100%;
			}
			#start-upload{
				display: none;
			}
		</style>
	</head>

	<body>
		<div class="mui-content">
			<a href="javascript:void(0)" id='imageupload' class='mui-btn mui-btn-green'>上传图片</a>
			<a href="javascript:void(0)" id='start-upload' class='mui-btn mui-btn-blue'>开始上传</a>
			<a href="javascript:void(0)" id='upload-twice' class='mui-btn mui-btn-danger'>清除缓存</a>
			<img src="" id="faceImg" />
			<div id="img1"></div>
			<div id="img2"></div>
		</div>
	</body>
	<script src="../js/mui.min.js"></script>
	<script type="text/javascript">
		(function($, doc) {
			mui.init();
			//console.log(md5);
			$.plusReady(function() {
				doc.getElementById('imageupload').addEventListener('tap', function() {
					plus.nativeUI.actionSheet({
						title: 'Ready!',
						cancel: 'cancel',
						buttons: [{
							//color: '#0062CC',
							title: '拍摄',
							style: 'default'
						}, {
							//color: '#0062CC',
							title: '从相册中选择',
							style: 'default'
						}]
					}, function(e) {
						//console.log(e.index);
						if(e.index == 1) {
							chooseCamera();
						}
						if(e.index == 2) {
							chooseGallery();
						}
					});

				});
				doc.getElementById('start-upload').addEventListener('tap',function(){
					var url,tsrc;
					var src=faceImg.getAttribute('src');
					if(src){
						if(tsrc && tsrc == src && url){
							return;
						}
						else{
							tsrc=src;
							uploadImag({
								url:'http://plrabbit.com/face/imageFile.php',
								filepath:src,
								datas:[{key:'key',value:src}],
								success:function(t){
									console.log(t.responseText);
									var img=JSON.parse(t.responseText);
									console.log(img.file);
									if(plus.storage.getItem('img1')){
										plus.storage.setItem('img2','http://plrabbit.com/face/'+img.file);
										doc.getElementById('img2').innerHTML=plus.storage.getItem('img2');
									}else{
										plus.storage.setItem('img1','http://plrabbit.com/face/'+img.file);
										doc.getElementById('img1').innerHTML=plus.storage.getItem('img1');
									}
									
								},
								failed:function(s){
									console.log('上传文件失败：'+s);
								}
							});
						}
					}else{
						$.alert('请选择图片！','未检测到图片','OK!');
					}
				});
				

				doc.getElementById('upload-twice').addEventListener('tap',function(){
					plus.storage.removeItem('img1');
					plus.storage.removeItem('img2');
					$.alert('清除成功!');
				})
				
				
				
			});

			function chooseGallery() {
				plus.gallery.pick(function(path) {
					faceImg.setAttribute('src', path);
					doc.getElementById('start-upload').style.display='inline-block';
				}, function(){}, {filter:'image'})
			}
			
			function chooseCamera() {
				var cmr = plus.camera.getCamera();//调用相机
				cmr.captureImage(function(p) { //IO流
					plus.io.resolveLocalFileSystemURL(p, function(entry) {
						faceImg.setAttribute('src', entry.toLocalURL());
						doc.getElementById('start-upload').style.display = 'inline-block';
					}, function(e) {});
				}, function(e) {}, {
					index: 1,
					filename: '_doc/camera/'
				});
			};
			
			
			function uploadImag(options){
				if(!options) return;
				var url=options.url,
					filepath=options.filepath,
					datas=options.datas || [],
					success=options.success,
					failed=options.failed;
				if(url && filepath){
					var task=plus.uploader.createUpload(url,{
						method:'POST',//上传文件都是用POST,不用get,容易造成文件损坏
						blocksize:204800,
						priority:100
					},function(t,status){
						if(status==200){
							if(success) success(t);
						}else{
							if(failed) failed(status);
						}
					});
					task.addFile(filepath,{key:'file'});
					if(datas && datas.length){//空数组返回的是true
						for(var i=0;i<datas.length;i++){
							var data=datas[i];
							task.addData(data.key,data.value);
						}
					}
					task.start();
				}
			}
			uploadImag({abc:123});
		})(mui, document)
	</script>

</html>